package com.hyy.controller;


import com.hyy.bean.MaskOrder;
import com.hyy.bean.MaskUser;
import com.hyy.bean.Merchandise;
import com.hyy.service.OrderService;
import com.hyy.service.serviceImpl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@WebServlet(name = "OrderServlet",urlPatterns = "/order")
public class OrderServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		req.setCharacterEncoding( "utf-8" ); //必须写在第一位，因为采用这种方式去读取数据，否则 数据会出错。
		//设置这样方式去读。这样中文就能够读取出来了，但是需要注意。表单的发送方式必须是method='post'
		resp.setContentType( "text/html;charset=utf-8" );

		Long uid = ((MaskUser)req.getSession().getAttribute("user")).getUserid();
		Double price = (Double) req.getSession().getAttribute("allPrice");
		String rcvname = req.getParameter("rcvname");
		String rcvphone = req.getParameter("rcvphone");
		String rcvprovince = req.getParameter("rcvprovince");
		String rcvcity = req.getParameter("rcvcity");
		String rcvdistrict = req.getParameter("rcvdistrict");
		String rcvzip = req.getParameter("rcvzip");
		String rcvaddress = req.getParameter("rcvaddress");

		//获取购物车商品及数量
		Map<Merchandise,Long> cartMerchandises = (Map<Merchandise,Long>)req.getSession().getAttribute("cartMerchandises");

		OrderService orderService = new OrderServiceImpl();
		String oid = String.valueOf(UUID.randomUUID());
		orderService.insertOrder(new MaskOrder(oid,uid,rcvname,rcvphone,rcvprovince,rcvcity,
				rcvdistrict,rcvaddress,rcvzip,price,0),oid,cartMerchandises);

		resp.sendRedirect("/");

	}
}
